//
//  MineFootView.swift
//  HLSwiftWork
//
//  Created by hulinhu on 2018/6/13.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit

// 一般情况下 如果是 可选类型 必须用（）包裹一下
// 正常情况下 闭包形式  ()->()
typealias btnClickBlock = (()->())?
//typealias btnClickBlock = (()->void)?  // 规范写法

class MineFootView: UIView {

    var finishedBlock : btnClickBlock
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.clear
        loadFootViewData()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
// MARK: - 网络请求
extension MineFootView {
    fileprivate func loadFootViewData() {
        MineFootViewModel.loadMineFooterData { [weak self] (footViewModelArray) in
            self?.setupButtons(footViewModelArray)
        }
    }
}
extension MineFootView {
    
 fileprivate func setupButtons(_ data:[MineFootModel]) {
    
        let maxCols: CGFloat = 4
        let buttonW = kScreenWidth / maxCols
        let buttonH = buttonW
        
        for i in 0..<data.count {
            
            let button = MineSqurebtn()
            button.content = data[i]
            // 行
            let row = i / Int(maxCols)
            // 列
            let col = CGFloat(i).truncatingRemainder(dividingBy: maxCols)
            let x = col * buttonW
            let y = CGFloat(row) * buttonH
            button.frame = CGRect(x: x, y: y, width: buttonW, height: buttonH)
            addSubview(button)
        }
        
        // 总页数 == (总个数 + 每页的最大数 - 1) / 每页最大数
        let rows = (CGFloat(data.count) + maxCols - 1) / maxCols
        frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: rows * buttonH)
    
        // 不为点击只是为了 回调刷新数据
        finishedBlock?()
    }
}
